package com.deng.Service.impl;

import com.deng.Service.ColumnService;
import com.deng.config.common.Paging;
import com.deng.config.common.TimeUtil;
import com.deng.mapper.ColumnMapper;
import com.deng.model.Column;
import com.deng.model.Paper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.common.collect.Maps;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Author 邓小熙
 * @Date 2022/5/13 1:51
 * @Version 1.0
 */
@Service
public class ColumnServiceImpl implements ColumnService {
    @Autowired
    ColumnMapper columnMapper;
    /**
     * 通过文章数量来推荐专题
     * 专题内文章由多到少排序
     * @return
     */
    @Override
    public List<Column> findColumnByPaperCount() {
        return columnMapper.findColumnByPaperCount();
    }
    /**
     * 查询所有主题
     * @param search
     * @param pageNum
     * @return
     */
    @Override
    public PageInfo<Column> findAllColumnSearch(String search, int pageNum) {
        PageHelper.startPage(pageNum,2);
        List<Column> list = columnMapper.findAllColumnSearch(search);
        PageInfo<Column> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }
    /**
     * 通过paper的id查询所属的column
     * @param id paperid
     * @return column
     */
    @Override
    public Column findColumnByPaperId(int id) {
        return columnMapper.findColumnByPaperId(id);
    }

    @Override
    public int updateColumnByPaperId(int paperId, String columnName) {
        int columnId = columnMapper.findColumnId(columnName);
//      根据paperId 查看存在不存在匹配的关系  先看存在不存在

        Column column = columnMapper.findColumnByPaperId(paperId);
        if(column == null){
            // 是空的那就插入 columnName
            return columnMapper.insertColumnPaper(columnId,paperId);
        }else{
            // 不为空才是修改
            return columnMapper.updateColumnByPaperId(paperId,columnId);
        }
    }
    /**
     *
     * @param page 页
     * @return
     */
    @Override
    public Map<String, Object> allColumn(String page) {
        int pageNum = Integer.parseInt(page);
        int limit = Paging.LIMIT.getValue();
        List<Column> columns = columnMapper.findAllColumnLimit((pageNum-1) * limit,limit);

        List<Column> columns1 = columnMapper.findAllColumn();
        Map<String,Object> map = Maps.newHashMap();
        map.put("code",0);
        map.put("mes","");
        map.put("count",columns1.size());
        map.put("data",columns);
        map.put("limit",limit);
        return map;
    }
    /**
     *
     * @param name 名称
     * @param image 图片名称
     * @return
     */
    @Override
    public int insertInlet(String name, String image) {
        Timestamp timestamp = TimeUtil.setCurrentTime();
        Column column = new Column();
        column.setImage(image);
        column.setTitle(name);
        column.setC_create_time(timestamp);
        return columnMapper.insertColumn(column);
    }
    /**
     * 通过ID删除专题
     * @param id
     * @return
     */
    @Override
    public int deleteColumnById(int id) {
        return columnMapper.deleteColumnById(id);
    }
    /**
     * 编辑专题(先通过ID查找专题)
     * @param id
     * @return
     */
    @Override
    public Column selectColumnById(int id) {
        return columnMapper.selectColumnById(id);
    }
    /**
     * 编辑专题
     * @param id
     * @return
     */
    @Override
    public int updateInlet(String name, String image, String id) {
        int id1 = Integer.parseInt(id);
        return columnMapper.updateColumnById(name,image,id1);
    }

    @Override
    public Set<String> findAllColumn() {
//        List<Column> columns = columnMapper.findAllColumn();
//        Map<String,Object> map = Maps.newHashMap();
//        map.put("theme",columns);
        return columnMapper.findAllColumns();
    }

    @Override
    public int findColumnId(String title) {
        return columnMapper.findColumnId(title);
    }
    /**
     * 为paper添加主题
     * @param columnId
     * @param paperId
     * @return
     */
    @Override
    public int insertColumnPaper(int columnId, int paperId) {
        return columnMapper.insertColumnPaper(columnId,paperId);
    }
    /**
     * 为paper添加主题
     * @param id
     * @param pageNum
     * @return
     */
    @Override
    public PageInfo<Paper> findPapersWithColumnId(int id, int pageNum) {
        PageHelper.startPage(pageNum,Paging.LIMIT.getValue());
        PageInfo<Paper> pageInfo = new PageInfo<>(columnMapper.findPapersWithColumnId(id));
        return pageInfo;
    }
}
